#1 导入依赖
from lxml import etree


#2.1 创建节点树
html_text = etree.parse("test.html",etree.HTMLParser())

#3 获取根节点html的元素
# bookname = html_text.xpath("//li/p[1]/text()")
# print(bookname)
# models = html_text.xpath("//li/p[2]/a/text()")
# print(models)
# prices = html_text.xpath("//li/p[3]/a/text()")
# print(prices)
# colors = html_text.xpath("//li/p[4]/text()")
# print(colors)

#第2种写法，用绝对路径，一个/
# bookname = html_text.xpath("/html/body/ul/li/p[1]/text()")
# print(bookname)
# models = html_text.xpath("/html/body/ul/li/p[2]/a/text()")
# print(models)
# prices = html_text.xpath("/html/body/ul/li/p[3]/a/text()")
# print(prices)
# colors = html_text.xpath("/html/body/ul/li/p[4]/text()")
# print(colors)

#筛选出http://localhost这个属性值
href = html_text.xpath('//*[@id="book_01"]/p[3]/a[@href="http://localhost"]')
print(href)
#筛选出凡是有href属性的a标签
href = html_text.xpath('//*[@id="book_01"]/p/a[@href]')
print(href)
#筛选出凡是有href属性的a标签，并显示href属性的值
href = html_text.xpath('//*[@id="book_01"]/p/a[@href]/@href')
print(href)
